﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using DG.Tweening;

namespace Jinndev {

    [RequireComponent(typeof(CanvasGroup))]
    public class MapLayerUI : MonoBehaviour {

        public int layerIndex;

        private CanvasGroup canvasGroup;

        private bool activate = false;
        public Dictionary<Int2, MapTileObjUI> tiles = new Dictionary<Int2, MapTileObjUI>();

        private void Awake() {
            canvasGroup = GetComponent<CanvasGroup>();
        }

        private void Update() {
            if (!activate) {
                return;
            }
        }

        public bool Active {
            get {
                return activate;
            }
            set {
                activate = value;
            }
        }

        public void RefreshAlpha(bool forceShow) {
            canvasGroup.alpha = (activate || forceShow) ? 1 : 0.5f;
        }

        public void Flash() {
            DOTweenUtil.DOFadeCanvasGroup(canvasGroup, 0.1f, 0.3f).SetLoops(4, LoopType.Yoyo); // 选中动画
        }

        /// <summary>
        /// 实例化并按遮挡关系计算插入位置，返回插入位置
        /// </summary>
        public int AddTile(MapObjData data, bool sort) {
            if (tiles.ContainsKey(data.index)) {
                return -1;
            }
            if (!MapUtil.tileConfigs.ContainsKey(data.id)) {
                Debug.LogError("瓦片配置不存在, id: " + data.id);
                return -1;
            }
            int siblingIndex = sort ? GetSiblingIndex(data) : transform.childCount;

            MapTileObjUI tile = CommonUtil.Instantiate<MapTileObjUI>(MapTileObjUI.PATH, transform);
            tile.transform.SetSiblingIndex(siblingIndex);
            tile.Refresh(data, layerIndex);

            tiles.Add(data.index, tile);
            return siblingIndex;
        }

        public bool RemoveTile(MapObjData data) {
            if (tiles.TryGetValue(data.index, out MapTileObjUI tile)) {
                tiles.Remove(data.index);
                Destroy(tile.gameObject);
                return true;
            }
            return false;
        }

        public MapTileObjUI GetTile(int siblingIndex) {
            return transform.GetChild(siblingIndex)?.GetComponent<MapTileObjUI>();
        }

        private int GetSiblingIndex(MapObjData data) {
            //foreach(var obj in tiles.Values) {
            //    obj.data.index
            //}
            for (int i = 0; i < transform.childCount; i++) {
                Transform child = transform.GetChild(i);
                MapTileObjUI obj = child.GetComponent<MapTileObjUI>();

                //Int2 delta = index - obj.data.index;

                if (data.index.y - data.height > obj.data.index.y - obj.data.height) {
                    return child.GetSiblingIndex();
                }
                //else if (index.y == obj.data.index.y && index.x > obj.data.index.x) {
                //    return child.GetSiblingIndex();
                //}
            }
            return transform.childCount;
        }

    }

}
